package com.sun.androidexerciseproject.function.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.os.Bundle;
import android.support.annotation.IntDef;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static android.content.pm.PackageManager.GET_META_DATA;
import static android.content.pm.PackageManager.GET_PROVIDERS;

/**
 * Created by Administrator on 2017/6/13.
 */

public final class PackageInfoUtil {
    public static final int TEST1 = 1;
    public static final int TEST2 = 2;
    public static final int TEST3 = 3;
    public static final int TEST4 = 4;
    private static Map<String, String> sMetaMap = new HashMap<>();

    public static Map<String, String> getMetaInfo(Context context) {
        try {
            ApplicationInfo info = context.getPackageManager().getApplicationInfo(context.getPackageName(), GET_META_DATA);
            if (null != info) {
                Bundle meta = info.metaData;
                if (null != meta) {
                    Set<String> set = meta.keySet();
                    if (null != set) {
                        for (String str : set) {
                            sMetaMap.put(str, meta.getString(str));
                        }
                    }
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return sMetaMap;
    }

    public static void getProvider(Context context) {
        try {
            ProviderInfo[] info = context.getPackageManager().getPackageInfo(context.getPackageName(), GET_PROVIDERS).providers;
            if (null != info) {
                int size = info.length;
                LogUtil.log("ProviderInfo size = " + size);
                for (ProviderInfo providerInfo : info) {
                    String authority = providerInfo.authority;
                    LogUtil.log("ProviderInfo authority = " + authority);
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void setAnnotationTest(@AnnotationTest int test) {

    }

    @IntDef(flag = true, value = {TEST1, TEST2, TEST3, TEST4})
    @Retention(RetentionPolicy.SOURCE)
    public @interface AnnotationTest {
    }
}
